/*
 * Copyright 2018-9-30 20:11:44 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.utils.DictionaryUtils;
import com.lap.scp.erp.ao.SuppilerAO;
import com.lap.scp.erp.domain.SuppilerDO;
import com.lap.scp.erp.query.SuppilerQueryVO;

/**
 * <pre>
 *  供应商信息，记录供应商信息，供应商的描述，加入时间等控制对象，提供 供应商信息，记录供应商信息，供应商的描述，加入时间等 新增,编辑,查询等操作。
 *  控制层统一命名规则：
 *  进入新增 	add,
 *  增加保存 	save,
 *  进入修改 	edit,
 *  修改保存 	update,
 *  查询分页 	queryPage,
 *  根据ID查询 	get,
 *  删除 		deleteById
 *
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-9-30 20:11:44
 * @since 2.0.0
 */
@Controller
@RequestMapping("suppiler")
public class SuppilerController {
	/**
	 * 供应商信息，记录供应商信息，供应商的描述，加入时间等应用流程对象
	 */
	@Resource
	private SuppilerAO suppilerAO;

	/**
	 * <pre>
	 * 进入新增供应商信息，记录供应商信息，供应商的描述，加入时间等
	 * </pre>
	 * 
	 * @return
	 */
	@GetMapping(value = "add")
	public ModelAndView add() {
		ModelAndView andView = new ModelAndView("erp/suppilerEdit");
		andView.addObject("suppilerDO", new SuppilerDO());
		andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
		return andView;
	}

	/**
	 * <pre>
	 * 新增保存供应商信息，记录供应商信息，供应商的描述，加入时间等
	 * </pre>
	 * 
	 * @param suppilerDO
	 * @param error
	 * @return
	 */
	@PostMapping(value = "add")
	public ModelAndView add(SuppilerDO suppilerDO, BindingResult error) {
		ModelAndView andView = new ModelAndView();

		Resp<SuppilerDO> resp = suppilerAO.insertSuppiler(suppilerDO, error);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/suppiler/list");
		} else {
			andView.setViewName("erp/suppilerEdit");
			andView.addObject("suppilerDO", suppilerDO);
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
		}
		return andView;
	}

	/**
	 * <pre>
	 * 根据ID删除供应商信息，记录供应商信息，供应商的描述，加入时间等
	 * </pre>
	 * 
	 * @param suppilerId
	 * @param map
	 * @param attributes
	 * @return
	 */
	@GetMapping(value = "delete/{suppilerId}")
	public ModelAndView deleteById(@PathVariable Integer suppilerId, @RequestParam Map<String, Object> map,
			RedirectAttributes attributes) {
		ModelAndView andView = new ModelAndView();

		Resp<Integer> resp = suppilerAO.deleteById(suppilerId);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/suppiler/list");
			attributes.addAllAttributes(map);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 根据ID查询供应商信息，记录供应商信息，供应商的描述，加入时间等
	 * </pre>
	 * 
	 * @param suppilerId
	 * @return
	 */
	@GetMapping(value = "get/{suppilerId}")
	public ModelAndView get(@PathVariable Integer suppilerId) {
		ModelAndView andView = new ModelAndView();

		Resp<SuppilerDO> resp = suppilerAO.queryById(suppilerId);
		if (resp.isSuccess()) {
			andView.setViewName("erp/suppilerDetail");
			andView.addObject("suppilerDO", resp.getData());
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 根据条件、分页信息查询供应商信息，记录供应商信息，供应商的描述，加入时间等
	 * </pre>
	 * 
	 * @param page
	 * @param queryVO
	 * @return
	 */
	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView queryPage(Page page, SuppilerQueryVO queryVO) {
		ModelAndView andView = new ModelAndView();

		PageResult<SuppilerDO> pr = suppilerAO.queryPage(page, queryVO);
		if (pr.isSuccess()) {
			andView.setViewName("erp/suppilerList");
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
			andView.addObject("queryVO", queryVO);
			andView.addObject("pr", pr);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 进入供应商信息，记录供应商信息，供应商的描述，加入时间等 编辑页面
	 * </pre>
	 * 
	 * @param suppilerId
	 * @return
	 */
	@GetMapping(value = "edit/{suppilerId}")
	public ModelAndView edit(@PathVariable Integer suppilerId) {
		ModelAndView andView = new ModelAndView();

		Resp<SuppilerDO> resp = suppilerAO.queryById(suppilerId);
		if (resp.isSuccess()) {
			andView.setViewName("erp/suppilerEdit");
			andView.addObject("suppilerDO", resp.getData());
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 编辑保存供应商信息，记录供应商信息，供应商的描述，加入时间等信息
	 * </pre>
	 * 
	 * @param suppilerId
	 * @return
	 */
	@PostMapping(value = "edit")
	public ModelAndView edit(SuppilerDO suppilerDO, BindingResult error, @RequestParam Map<String, String> map,
			RedirectAttributes attributes) {
		ModelAndView andView = new ModelAndView();

		Resp<Integer> resp = suppilerAO.updateSuppiler(suppilerDO, error);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/suppiler/list");
			attributes.addAllAttributes(map);
		} else {
			andView.setViewName("erp/suppilerEdit");
			andView.addObject("suppilerDO", suppilerDO);
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
		}
		return andView;
	}

	@GetMapping(value = "queryList")
	@ResponseBody
	public Resp<List<SuppilerDO>> queryList() {
		return suppilerAO.queryList();
	}

}
